import type { Player, CreatePlayerRequest, UpdatePlayerRequest, PlayerStatistics, PlayerSearchParams, PaginatedResponse } from '@dx-fun-group/shared-types';
import { PlayerPosition, PlayerRank } from '@dx-fun-group/shared-types';
import api from './api';

// 模拟数据 - 在实际项目中这些数据会来自后端API
const mockPlayers: Player[] = [
  {
    id: '1',
    nickname: '大神玩家',
    positions: [PlayerPosition.CARRY, PlayerPosition.MID],
    rank: PlayerRank.LEGEND,
    highestMmr: 4100,
    ability: 5,
    tags: ['操作细腻', '意识优秀', '团队配合好'],
    createdAt: '2024-01-01T10:00:00Z',
    updatedAt: '2024-01-01T10:00:00Z',
    behaviorScore: 10
  },
  {
    id: '2',
    nickname: '辅助专家',
    positions: [PlayerPosition.SUPPORT, PlayerPosition.HARD_SUPPORT],
    rank: PlayerRank.ANCIENT,
    highestMmr: 4800,
    ability: 4,
    tags: ['视野控制', '保护意识强', '经济分配合理'],
    createdAt: '2024-01-02T10:00:00Z',
    updatedAt: '2024-01-02T10:00:00Z',
    behaviorScore: 9
  },
  {
    id: '3',
    nickname: '新手玩家',
    positions: [PlayerPosition.OFFLANE],
    rank: PlayerRank.HERALD,
    highestMmr: 650,
    ability: 2,
    tags: ['学习能力强', '态度积极'],
    createdAt: '2024-01-03T10:00:00Z',
    updatedAt: '2024-01-03T10:00:00Z',
    behaviorScore: 8
  }
];

// let nextId = 4; // 保留供将来模拟数据使用

// 模拟API延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

// 获取所有选手（保留兼容性）
export const getAllPlayers = async (): Promise<Player[]> => {
  try {
    // 实际API调用
    // const response = await api.get<ApiResponse<Player[]>>('/players');
    // return response.data.data;
    
    // 模拟API调用
    await delay(500);
    return [...mockPlayers];
  } catch (error) {
    console.error('获取选手列表失败:', error);
    throw error;
  }
};

// 分页搜索选手
export const searchPlayers = async (params: PlayerSearchParams = {}): Promise<PaginatedResponse<Player>> => {
  try {
    // 实际API调用
    const response = await api.get<PaginatedResponse<Player>>('/players', { params });
    return response.data;
  } catch (error) {
    console.error('搜索选手失败:', error);
    throw error;
  }
};

// 根据ID获取选手
export const getPlayerById = async (id: string): Promise<Player | null> => {
  try {
    // 实际API调用
    const response = await api.get<{success: boolean; data: Player; message: string}>(`/players/${id}`);
    return response.data.data;
  } catch (error) {
    console.error('获取选手详情失败:', error);
    return null;
  }
};

// 创建新选手
export const createPlayer = async (data: CreatePlayerRequest): Promise<Player> => {
  try {
    // 实际API调用
    const response = await api.post<{success: boolean; data: Player; message: string}>('/players', data);
    return response.data.data;
  } catch (error) {
    console.error('创建选手失败:', error);
    throw error;
  }
};

// 更新选手信息
export const updatePlayer = async (id: string, data: UpdatePlayerRequest): Promise<Player | null> => {
  try {
    // 实际API调用
    const response = await api.put<{success: boolean; data: Player; message: string}>(`/players/${id}`, data);
    return response.data.data;
  } catch (error) {
    console.error('更新选手失败:', error);
    return null;
  }
};

// 删除选手
export const deletePlayer = async (id: string): Promise<boolean> => {
  try {
    // 实际API调用
    await api.delete<{success: boolean; data: null; message: string}>(`/players/${id}`);
    return true;
  } catch (error) {
    console.error('删除选手失败:', error);
    return false;
  }
};


// 根据位置筛选选手
export const getPlayersByPosition = async (position: PlayerPosition): Promise<Player[]> => {
  try {
    // 实际API调用
    // const response = await api.get<ApiResponse<Player[]>>(`/players/filter?position=${position}`);
    // return response.data.data;
    
    // 模拟API调用
    await delay(300);
    return mockPlayers.filter(player => player.positions.includes(position));
  } catch (error) {
    console.error('按位置筛选选手失败:', error);
    throw error;
  }
};

// 根据段位筛选选手
export const getPlayersByRank = async (rank: string): Promise<Player[]> => {
  try {
    // 实际API调用
    // const response = await api.get<ApiResponse<Player[]>>(`/players/filter?rank=${rank}`);
    // return response.data.data;
    
    // 模拟API调用
    await delay(300);
    return mockPlayers.filter(player => player.rank === rank);
  } catch (error) {
    console.error('按段位筛选选手失败:', error);
    throw error;
  }
};

// 获取选手统计信息
export const getPlayerStatistics = async (): Promise<PlayerStatistics> => {
  try {
    // 实际API调用
    const response = await api.get<{success: boolean; data: PlayerStatistics; message: string}>('/statistics/players');
    return response.data.data;
  } catch (error) {
    console.error('获取选手统计失败:', error);
    throw error;
  }
};
